package com.hibernate.model;

import java.io.Serializable;
import java.util.*;

public class Customer implements Serializable {

    private Long id;
    private String firstname;
    private String lastname;
    private char sex;
    private Set orders = new HashSet();
    private Double avgPrice;
    private Double totalPrice;
    private String description;

    public Customer() {
    }

    public Customer(String firstname, String lastname, char sex, Set orders, String description) {
        this.firstname = firstname;
        this.lastname = lastname;
        this.sex = sex;
        this.orders = orders;
        this.description = description;
    }
    public Long getId() {
        return this.id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getFirstname() {
        return firstname;
    }

    public void setFirstname(String firstname) {
        this.firstname = firstname;
    }

    public String getLastname() {
        return lastname;
    }

    public void setLastname(String lastname) {
        this.lastname = lastname;
    }

    public String getName() {
        return firstname + " " + lastname;
    }

    public void setName(String name) {
        StringTokenizer t = new StringTokenizer(name);
        firstname = t.nextToken();
        lastname = t.nextToken();
    }

    public Double getAvgPrice() {
        return this.avgPrice;
    }
    private void setAvgPrice(Double avgPrice) {
        this.avgPrice = avgPrice;
    }
    public Double getTotalPrice() {
        return this.totalPrice;
    }
    private void setTotalPrice(Double totalPrice) {
        this.totalPrice = totalPrice;
    }

    public void setOrders(Set orders) {
        this.orders = orders;
        calculatePrice();
    }
    public Set getOrders() {
        return orders;
    }
    private void calculatePrice() {
        double avgPrice = 0.0;
        double totalPrice = 0.0;
        int count = 0;

        if (getOrders() != null) {
            Iterator iter = getOrders().iterator();
            while (iter.hasNext()) {
                double orderPrice = ((Order) iter.next()).getPrice();
                totalPrice += orderPrice;
                count++;
            }
            avgPrice = totalPrice / count;
            setAvgPrice(new Double(avgPrice));
        }
    }
    public char getSex() {
        return this.sex;
    }
    public void setSex(char sex) {
        if (sex != 'F' && sex != 'M') {
            throw new IllegalArgumentException("Invalid Sex");
        }
        this.sex = sex;
    }

    public String getDescription() {
        return this.description;
    }
    public void setDescription(String description) {
        this.description = description;
    }
    @Override
    public String toString() {
        return "Customer{" +
                "id=" + id +
                ", firstname='" + firstname + '\'' +
                ", lastname='" + lastname + '\'' +
                ", sex=" + sex +
                ", orders=" + orders +
                ", avgPrice=" + avgPrice +
                ", totalPrice=" + totalPrice +
                ", description='" + description + '\'' +
                '}';
    }
}
